package com.qf.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.dao.DrugMapper;
import com.qf.dao.DrugOrderItemMapper;
import com.qf.dao.DrugOrderMapper;
import com.qf.pojo.entity.MedicleDrug;
import com.qf.pojo.entity.MedicleOrder;
import com.qf.pojo.entity.MedicleOrderitem;
import com.qf.pojo.req.ReqDrug;
import com.qf.pojo.req.ReqDrugOrder;
import com.qf.pojo.resp.Resultmsg;
import com.qf.service.DrugOrderService;
import com.qf.util.UserUtil;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;
import java.util.Set;

/**
 * @ClassName DrugOrderServiceImpl
 * @Description
 * @Author lanzhike
 * @Date 2023/8/15/0015 上午 09:25
 * @Version 1.0
 * 正常购买业务,不需要考虑并发场景
 */
@Service
public class DrugOrderServiceImpl implements DrugOrderService {

    @Autowired
    DrugOrderItemMapper drugOrderItemMapper;
    @Autowired
    DrugMapper drugMapper;
    @Autowired
    DrugOrderMapper drugOrderMapper;
    @Override
    public Resultmsg addDrugOrder(ReqDrugOrder drugOrder, HttpServletRequest request) {
        String userId = UserUtil.getUserId(request);
        //生成订单
        MedicleOrder medicleOrder = new MedicleOrder();
        medicleOrder.setCreateTime(new Date());
        medicleOrder.setOrderFromaddr(drugOrder.getFromAddr());
        medicleOrder.setOrderToaddr(drugOrder.getToAddr());
        medicleOrder.setPayType(drugOrder.getPayType());
        long transactionId = IdUtil.getSnowflakeNextId();
        medicleOrder.setTransactionId(String.valueOf(transactionId));
        drugOrderMapper.insert(medicleOrder);
        //生成订单项
        medicleOrder.setUserId(Integer.valueOf(userId));
        MedicleOrderitem medicleOrderitem = new MedicleOrderitem();
        Map<MedicleDrug, Integer> drugOrderItems = drugOrder.getDrugOrderItems();
        Set<MedicleDrug> medicleDrugs = drugOrderItems.keySet();
        for (MedicleDrug medicleDrug : medicleDrugs) {
            Integer num = drugOrderItems.get(medicleDrug);
            //查询数据库,判断库存是否足够
            QueryWrapper<MedicleDrug> wrapper = new QueryWrapper<>();
            wrapper.eq("drug_id",medicleDrug.getDrugId());
            MedicleDrug medicleDrug1 = drugMapper.selectOne(wrapper);
            if (medicleDrug1.getDrugNum() < num){
                return Resultmsg.fail(medicleDrug1.getDrugName()+"库存不足,请减少购买个数");
            }
            medicleOrderitem.setDrugId(medicleDrug.getDrugId());
            medicleOrderitem.setOrderitemMoney(String.valueOf(medicleDrug.getDrugPrice()*num));
            medicleOrderitem.setOrderitemNum(num);
            medicleOrderitem.setUserId(Integer.valueOf(userId));
            medicleOrderitem.setTransactionId(String.valueOf(transactionId));
            drugOrderItemMapper.insert(medicleOrderitem);
        }
        //调用支付接口
        return Resultmsg.success();
    }
}
